import { bns, axios } from "@aife/aife-base";
import Vue from "vue";
const vm = new Vue();
const auth = bns.auth;
axios.defaults.baseURL = `/console/`;

// 拦截的过滤列表，此处放api的名字 比如 get_apiKey_list 等
const intercept_filter_list = [];
// 大数据接口拦截列表   大数据接口如果报错，直接屏蔽，不往下执行
const bigdata_filter_list = [];

// 轮询接口过滤，依照路径过滤
const roll_filter_url = [];
// 轮询接口过滤，依照$$apiname过滤
const roll_filter_apiname = [""];

// 过滤掉 不报错的code
const no_report_error_code = [];

// 不触发loading动作的接口
const no_loading = [];

class Loading {
  constructor() {
    this.loadingService = null;
    this.loadingCount = 0;
  }
  create() {
    this.loadingService = vm.$loading({
      lock: true,
      text: "拼命加载中...",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.5)"
    });
  }
  close() {
    this.loadingService && this.loadingService.close && this.loadingService.close();
  }
  checkStatus() {
    this.loadingCount--;
    setTimeout(() => {
      !this.loadingCount && this.close();
    });
  }
  addCount() {
    this.create();
    this.loadingCount++;
  }
}

let loading = new Loading();

function handleLoading(callback, config) {
  let apiname = config.$$apiname;
  if (apiname && !no_loading.includes(apiname)) {
    callback();
  }
}

axios.interceptors.request.use(
  function(config) {
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);

axios.interceptors.response.use(auth.update(), error => Promise.reject(error));

axios.interceptors.response.use(
  function(response) {
    // handleLoading(loading.checkStatus.bind(loading),response.config)
    // 这一部分代码用于处理 组件中取数据是根据response.data.result去取得， 解决组件的兼容问题
    var resData = response.data;
    if (resData.data === undefined) {
      resData = {
        data: response.data,
        ...response.data
      };
    }
    // response.config.$$apiname 保证了 调用的接口非外部组件的接口
    let innerPort = !!response.config.$$apiname;

    if (innerPort && resData.code == 201702) {
      console.log(innerPort, resData, resData.code, "/console/home/index.html#/overview");
      window.location.href = "/console/home/index.html#/overview";
      return new Promise(() => {});
    }
    // if(innerPort&&resData.code == 401){
    // 	vm.Alert({
    // 		type: 'warning',
    // 		message:'暂无权限，如需访问请联系项目组管理员修改权限'
    // 	})
    // 	return new Promise(()=>{})
    // }

    // 401 和 0 不进行拦截 用户没有权限的控制 应该交由v-permission去做
    let inValidCode =
      (response.data.code && response.data.code != "0" && response.data.code != "401") ||
      (response.data.error_code && response.data.error_code != "0");

    // 是否在拦截 的过滤列表当中
    let beFiltered = intercept_filter_list.some(item => response.config.$$apiname === item);

    // 是否在大数据拦截列表中
    let bigdataFiltered = bigdata_filter_list.some(item => response.config.$$apiname === item);

    if (innerPort && inValidCode && !beFiltered) {
      let noReportError = no_report_error_code.some(item => response.data.code == item);

      !noReportError &&
        vm.Alert({
          type: "error",
          message: response.data.message || "服务器错误"
        });
      return new Promise(() => {});
    }

    if (innerPort && inValidCode && bigdataFiltered) {
      return new Promise(() => {});
    }

    return resData;
  },
  function(error) {
    let config = error.config;
    let bigdataFiltered = bigdata_filter_list.some(item => config.$$apiname === item);
    if (
      roll_filter_url.some(item => item == config.url) ||
      roll_filter_apiname.some(item => item == config.$$apiname) ||
      !config.$$apiname ||
      bigdataFiltered
    ) {
      return Promise.reject(error);
    }

    vm.Alert({
      type: "error",
      message: "网络错误"
    });
    return Promise.reject(error);
  }
);
